/* 
 *       __        __           _______    _ 
 *      / / __    / /  __   __  \_  __/   |_|    ___     _____
 * 	   / / /  \  / /  / /  / /   / /     / /    / _ |   |  _  |  
 *    / /_/ /\ \/ /  / /__/ /   / /     / /    / __ |   / | | |
 *    \____/  \__/   \_____/   / /     /___\  /_/ | |  /_/  |_|
 *                      __    / /                 |/
 *                      \ \__/ /
 *                       \____/
 * 
 */

package org.wltea.analyzer.lucene;

import java.io.IOException;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.analysis.util.FilteringTokenFilter;
import org.apache.lucene.util.Version;

/** 
 * @version 创建时间：2013-8-12 下午1:59:19 
 */
public class SingleWordFilter extends FilteringTokenFilter {
	
	private final CharArraySet singleWord;
	private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);

	public SingleWordFilter(boolean enablePositionIncrements, TokenStream input,
			CharArraySet stopWords) {
		super(Version.LUCENE_47, input);

		this.singleWord = stopWords;
	}

	@Override
	protected boolean accept() throws IOException {
		int len = termAtt.length();
//		System.out.println(termAtt.toString()+"-"+len);
//		if(len < 2) {
//			if(singleWord.contains(termAtt.buffer(), 0, len)) {
//				System.out.println("true : " + termAtt);
//				return true;
//			}
//			System.out.println("false : " + termAtt);
//			return false;
//		}
//		// System.out.println("<IKStopFilter>accept()"+termAtt.toString());
////		return !singleWord.contains(termAtt.buffer(), 0, termAtt.length()); // 未被赋值过？隐藏操作在哪里实现？
//		System.out.println("true : " + termAtt);
		
		if(len > 1) {
			return true;
		}

		return singleWord.contains(termAtt.buffer(), 0, len);
	}
}
